package com.carkeeper.caruser.contorller;

import com.carkeeper.caruser.pojo.User;
import com.carkeeper.caruser.service.UserService;
import com.carkeeper.entity.PageResult;
import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(value = "车主")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 分页查询所有车主信息
     * @param currentpage
     * @param pagesize
     * @return
     */
    @ApiOperation(value = "分页查询所有车主信息")
    @ApiImplicitParams({
           @ApiImplicitParam(name = "currentpage",value = "当前页"),
           @ApiImplicitParam(name = "pagesize",value = "每页条数")
    })
    @GetMapping("/PageUser")
    public Result<User> PageUser(@RequestParam(name = "currentpage",required = false)Integer currentpage, @RequestParam(name = "pagesize",required = false)Integer pagesize) {
        PageInfo<User> userPageResult = userService.PageUser(currentpage, pagesize);
        return new Result<User>(true,StatusCode.OK,"查询成功",userPageResult);

    }

    /**
     * 多条件查询
     * @param map
     * @return
     */
    @ApiOperation(value = "多条件查询")
    @GetMapping("/searchUser")
    public Result<User> searchUser(@RequestParam() Map map) {
        PageInfo<User> userPageResult = userService.searchUser(map);
        return new Result<User>(true,StatusCode.OK,"查询成功",userPageResult);
    }

    /**
     * 锁定
     * @param userId
     * @return
     */
    @ApiOperation(value = "锁定")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",required = true),
            @ApiImplicitParam(name = "status",value = "0为锁定，1为解锁",required = true)
    })
    @PutMapping("/updateUserStatus")
    public Result updateUserStatus(@RequestParam("userId") BigInteger userId,@RequestParam("status") Character status) {
        int count=userService.updateUserStatus(userId,status);
        if (count <= 0) {
            return new Result(false, StatusCode.ERROR, "失败");
        } else {
            return new Result(true, StatusCode.OK, "成功");
        }
    }

    /**
     * 根据id查询车主
     * @param userId
     * @return
     */
    @ApiOperation(value = "根据id查询车主")
    @ApiImplicitParam(name = "userId",value = "用户id",required = true)
    @GetMapping("/findUserById")
    public Result<User> findUserById(@RequestParam("userId") String userId) {
        User user = userService.findUserById(userId);

        if (user != null) {
            return new Result(true, StatusCode.OK, "查询成功",user);
        } else {
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

}
